import request from '@/utils/request';

const REQUEST_TIMEOUT = 15000;

// 发布
export function saveWorkFlow(params) {
    const { CancelToken } = request;
    let cancelRequest;
    let setTimeoutId;
    return new Promise(resolve => {
        clearTimeout(setTimeoutId);
        setTimeoutId = setTimeout(() => {
            cancelRequest('网关超时!');
        }, REQUEST_TIMEOUT);

        request('/api/workflowweb/deploy/v1/processdeploy', {
            method: 'POST',
            data: params,
            cancelToken: new CancelToken(c => {
                cancelRequest = c;
            })
        })
            .then(result => {
                resolve(result);
            })
            .finally(() => {
                clearTimeout(setTimeoutId);
            });
    });
}

// 暂存
export function saveProcessHold(params) {
    const { CancelToken } = request;
    let cancelRequest;
    let setTimeoutId;
    return new Promise(resolve => {
        clearTimeout(setTimeoutId);
        setTimeoutId = setTimeout(() => {
            cancelRequest('网关超时!');
        }, REQUEST_TIMEOUT);

        request('/api/workflowweb/deploy/v1/processHold', {
            method: 'POST',
            data: params,
            cancelToken: new CancelToken(c => {
                cancelRequest = c;
            })
        })
            .then(result => {
                resolve(result);
            })
            .finally(() => {
                clearTimeout(setTimeoutId);
            });
    });
}

// 模板列表
export function queryTempList(params) {
    return request('/api/workflowweb/deploy/v1/getgroupbycategory', {
        method: 'POST',
        data: params
    });
}

// 获取审批人配置
export function fetchApproveConfig() {
    return request('/api/workflow/approve/v1/getapproveconfig', {
        method: 'GET'
    });
}

// 动态获取组织架构配置
export function fetchOrgByUrl(params) {
    const { url } = params;
    return request(`${url}`, {
        method: 'GET'
    });
}

export function queryWorkFlow(id) {
    return request(`/api/workflow/deploy/v1/getprocessbydeploymentid?processDeploymentId=${id}`, {
        method: 'GET'
    });
}
// 申请
export function queryApplyForm(params) {
    return request('/api/workflowweb/processinstance/v1/getformdata', {
        method: 'POST',
        data: params
    });
}
// 申请提交
export function saveApplyForm(params) {
    return request('/api/workflowweb/processinstance/v1/start', {
        method: 'POST',
        data: params
    });
}

// 流程审批人预览
export function queryApprove(params) {
    return request('/api/workflowweb/approve/v1/navigate/list', {
        method: 'POST',
        data: params
    });
}

// 更新流程定义状态:禁用、启用
export function updateProcessdefStatus(params) {
    return request('/api/workflowweb/deploy/v1/updateprocessdefstatus', {
        method: 'POST',
        data: params
    });
}

// 更新流程定义状态:禁用、启用
export function deleteProcess(id) {
    return request(`/api/workflowweb/deploy/v1/deleteProcess/${id}`, {
        method: 'DELETE'
    });
}

// 通过id获取审批内容
export function getPostApplyData(id) {
    return request(`/api/oa/workflow/business/v1/${id}/getBusinessData`);
}

export function getQueryBusinessData(id) {
    return request(`/api/oa/workflow/business/v1/${id}/queryBusinessData`);
}

// 获取流程定义
export async function getFlowByType(type) {
    return request(`/api/workflowweb/processinstance/v1/getByAdvancedSettingType?classification=${type}`, {
        method: 'GET'
    });
}
